package sg.edu.nus.iss.billing.ui.gui;


import java.util.*;

import javax.swing.*;

import sg.edu.nus.iss.billing.ui.controller.OneBillingSystem;
import sg.edu.nus.iss.billing.vo.subscription.DigitalVoiceVO;
import sg.edu.nus.iss.billing.vo.subscription.MobileVoiceVO;
import sg.edu.nus.iss.billing.vo.subscription.SubscriptionVO;
import sg.edu.nus.iss.billing.vo.subscription.feature.FeatureVO;

import java.awt.Dimension;
import java.awt.FlowLayout;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyVetoException;


public class DigitalVoicePanel  extends JPanel {

	private static final long serialVersionUID = 1L;
	private TabbedFrame parent;
	private OneBillingSystem onebillingsystem;
	private DigitalVoiceVO digitalvoiceVO;
	private DigitalVoiceVO subscribeddigitalvoiceVO;
	private ArrayList<FeatureVO> subscribedoptionalfeatures;
	
	JPanel selectionPanel;
	JPanel basicFeaturePanel;
	JPanel optionalFeaturePanel;
	JPanel buttonPanel;
	
	JCheckBox chkexclude;
	
	public DigitalVoicePanel(TabbedFrame parent, OneBillingSystem onebillingsystem){
		this.parent= parent;
		this.onebillingsystem=onebillingsystem;
		
		Iterator<SubscriptionVO> iterator = parent.getAvailableplanoptions().iterator();
		SubscriptionVO plan;
		while (iterator.hasNext()) {
			plan= iterator.next();
			if( plan instanceof DigitalVoiceVO){
				digitalvoiceVO = (DigitalVoiceVO) plan;
			}
		}
		
		Iterator<SubscriptionVO> iterator2 = parent.getSubscripedplanoptions().iterator();
		System.out.println("--Subscribed Length----");
		System.out.println(parent.getSubscripedplanoptions().size());
		SubscriptionVO plan2;
		while (iterator2.hasNext()) {
			plan2= iterator2.next();
			if( plan2 instanceof DigitalVoiceVO){
				subscribeddigitalvoiceVO = (DigitalVoiceVO) plan2;
				System.out.println(subscribeddigitalvoiceVO.toString());
				System.out.println("---11---");
				System.out.println(subscribeddigitalvoiceVO.getName());
				System.out.println("---21---");
				System.out.println(subscribeddigitalvoiceVO.getMonthlySuscription());
				System.out.println("---31---");
				System.out.println(subscribeddigitalvoiceVO.getAllOptionalFeature().toArray().length);
			}
		}
		if(subscribeddigitalvoiceVO==null){
			subscribeddigitalvoiceVO = new DigitalVoiceVO();
		}
		subscribedoptionalfeatures= subscribeddigitalvoiceVO.getAllOptionalFeature();
		
		selectionPanel=createSelectionPanel();
		basicFeaturePanel=createBasicFeaturePanel();
		optionalFeaturePanel=createOptionalFeaturePanel();
		buttonPanel=createButtonPanel();
		
		 GroupLayout layout = new GroupLayout(this);
	        setLayout(layout);
	        layout.setHorizontalGroup(
	            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
	            .addGroup(layout.createSequentialGroup()
	                //.addGap(24, 24, 24)
	                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
	                    .addComponent(selectionPanel, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
	                    .addComponent(basicFeaturePanel, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
	                    .addComponent(optionalFeaturePanel, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
	                    .addComponent(buttonPanel, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
	                .addContainerGap(108, Short.MAX_VALUE))
	        );
	        layout.setVerticalGroup(
	            layout.createParallelGroup(GroupLayout.Alignment.LEADING)
	            .addGroup(layout.createSequentialGroup()
	               // .addGap(29, 29, 29)
	                .addComponent(selectionPanel, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
	                .addComponent(basicFeaturePanel, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
	                .addComponent(optionalFeaturePanel, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
	                .addComponent(buttonPanel, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
	                .addContainerGap(214, Short.MAX_VALUE))
	        );
			
	}
	
	private void performchkboxChange(ActionEvent e){
		JCheckBox checkbox = (JCheckBox)e.getSource();
		System.out.println(checkbox.getText());
		System.out.println(checkbox.isSelected());
		if(checkbox.isSelected()){
			Iterator<FeatureVO> iterator = digitalvoiceVO.getAllOptionalFeature().iterator();
			while(iterator.hasNext()){
				FeatureVO optfeature = iterator.next();
				System.out.println("DVP:performchkboxChange + "+ optfeature.getName());
				if(optfeature.getName().equals(checkbox.getText())){
					subscribedoptionalfeatures.add(optfeature);
					subscribeddigitalvoiceVO.addOptionalFeatures(optfeature);
					break;
				}
			}
			System.out.println("DIGITALVOICEPANEL  performchkboxChange()  subscribedoptionalfeatures.size() " +subscribedoptionalfeatures.size());
		}
		else{
			Iterator<FeatureVO> iterator = subscribedoptionalfeatures.iterator();
			while(iterator.hasNext()){
				FeatureVO optfeature = iterator.next();
				if(optfeature.getName().equals(checkbox.getText())){
					subscribedoptionalfeatures.remove(optfeature);
					break;
				}
			}

		}
		subscribeddigitalvoiceVO.addOptionalFeatures(subscribedoptionalfeatures);
		parent.setSubscripedplanoptions(parent.getTab().getSelectedIndex(),subscribeddigitalvoiceVO);
	}
	
	private void performchkexcludeChange(){
		if(!chkexclude.isSelected()){
        	basicFeaturePanel.setVisible(true);
        	optionalFeaturePanel.setVisible(true);
        	parent.setSubscripedplanoptions(parent.getTab().getSelectedIndex(),subscribeddigitalvoiceVO);
        }
        else{
            	basicFeaturePanel.setVisible(false);
            	optionalFeaturePanel.setVisible(false);
            	parent.getHtsubscripedplanoptions().remove(parent.getTab().getSelectedIndex());
        	}
	}
	
	private JPanel createSelectionPanel( ){
		JPanel p = new JPanel ();
		p.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)),""));
		
		chkexclude=new JCheckBox("Exculde Digital Voice ");
		p.add(chkexclude);
		p.setPreferredSize(new Dimension(350, 60));
		
		chkexclude.addActionListener (new ActionListener () {
	            public void actionPerformed (ActionEvent e) {
	            	performchkexcludeChange();
	            		
	            }
	        });
		 
		 GroupLayout layout = new GroupLayout(p);
	        p.setLayout(layout);
	        layout.setHorizontalGroup(
	            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
	            .addGroup(layout.createSequentialGroup()
	                .addGap(15, 15, 15)
	                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
	                    .addComponent(chkexclude, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
	                .addContainerGap(20, Short.MAX_VALUE))
	        );
	        layout.setVerticalGroup(
	            layout.createParallelGroup(GroupLayout.Alignment.LEADING)
	            .addGroup(layout.createSequentialGroup()
	                .addGap(15, 15, 15)
	                .addComponent(chkexclude, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
	                .addContainerGap(20, Short.MAX_VALUE))
	        );
		return p;
	}
	
	private JPanel createBasicFeaturePanel(){
		JPanel p = new JPanel ();
		p.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)),"Basic Feature"));
		List<JCheckBox> checkboxlist = new ArrayList<JCheckBox>();
		JCheckBox checkbox;
		checkbox= new JCheckBox(digitalvoiceVO.getLocalCallFeatureVO().getName(),true);
		checkbox.setEnabled(false);
		checkboxlist.add(checkbox);

		 GroupLayout layout = new GroupLayout(p);
	        p.setLayout(layout);
	        layout.setHorizontalGroup(
	            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
	            .addGroup(layout.createSequentialGroup()
	                .addGap(24, 24, 24)
	                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
	                    .addComponent(checkboxlist.get(0), GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
	                .addContainerGap(108, Short.MAX_VALUE))
	        );
	        layout.setVerticalGroup(
	            layout.createParallelGroup(GroupLayout.Alignment.LEADING)
	            .addGroup(layout.createSequentialGroup()
	                .addGap(29, 29, 29)
	                .addComponent(checkboxlist.get(0), GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
	                .addContainerGap(214, Short.MAX_VALUE))
	        );
	    p.setPreferredSize(new Dimension(350, 100));
		return p;
	}
	
	private JPanel createOptionalFeaturePanel(){
		JPanel p = new JPanel ();
		p.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)),"Optional Features"));

		List<JCheckBox> checkboxlist = new ArrayList<JCheckBox>();
		JCheckBox checkbox;
		
		Iterator<FeatureVO> iterator = digitalvoiceVO.getAllOptionalFeature().iterator();
		while(iterator.hasNext()){
			FeatureVO feature;
			feature= iterator.next();
			System.out.println("In createOptionalFeaturePanel");
			System.out.println(feature.getDescription());
			System.out.println(feature.getName());
			checkbox= new JCheckBox(feature.getName(), false);
			checkbox.setEnabled(true);
			checkboxlist.add(checkbox);
			Iterator<FeatureVO> iterator2 = subscribeddigitalvoiceVO.getAllOptionalFeature().iterator();
			while(iterator2.hasNext()){
				FeatureVO feature2;
				feature2= iterator2.next();
				if(feature2.getName().equalsIgnoreCase(feature.getName()))
					checkbox.setSelected(true);
			}
			checkbox.addActionListener (new ActionListener () {
	            public void actionPerformed (ActionEvent e) {
	            	performchkboxChange(e);
	            }
	        });
			
		}

		 GroupLayout layout = new GroupLayout(p);
	        p.setLayout(layout);
	        layout.setHorizontalGroup(
	            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
	            .addGroup(layout.createSequentialGroup()
	            		 .addGap(24, 24, 24)
	                    .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
	                        .addComponent(checkboxlist.get(0)))
	                        //.addComponent(checkboxlist.get(2)))
	                    .addGap(24, 24, 24)
	                    .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
	                    		.addComponent(checkboxlist.get(1)))
	                    		 .addContainerGap(214, Short.MAX_VALUE))
	        );
	        layout.setVerticalGroup(
	            layout.createSequentialGroup()//(GroupLayout.Alignment.LEADING)
	            .addGroup(layout.createSequentialGroup()
	                    .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
	                        .addComponent(checkboxlist.get(0))
	                        .addComponent(checkboxlist.get(1)))
	                    //.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
	                        //.addComponent(checkboxlist.get(2)))
	                        )

	        );
	    p.setPreferredSize(new Dimension(350, 220));
		return p;
	}
	
	private JPanel createButtonPanel () {

        JPanel p = new JPanel ();

        JButton nextbutton = new JButton ("Next");
        nextbutton.addActionListener (new ActionListener () {
            public void actionPerformed (ActionEvent e) {
            	parent.getTab().setSelectedIndex(parent.getTab().getSelectedIndex()+1);
            }
        });
        p.add (nextbutton);
        
        JButton close = new JButton ("Close");
        close.addActionListener (new ActionListener () {
            public void actionPerformed (ActionEvent e) {
            	try {
					parent.setClosed(true);
				} catch (PropertyVetoException e1) {

				}
            }
        });
        p.add (close);

        JPanel bp = new JPanel ();
        bp.setLayout (new FlowLayout());
        bp.add (p);
        return bp;
    }

}
